package com.gonsin.license;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Component
public class LicenseInterceptor implements HandlerInterceptor {

    @Autowired private LicenseConfig licenseConfig;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

        /**
         * 如果未授权，统一跳转拿到授权页面
         */
        if(licenseConfig.isLicensed()){
            return true;
        }
        httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/license");
        return false;
    }

}

